package edu.uwlax.cs.oayonlinestore.client.gui.salesdept;

import java.util.Iterator;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;

import edu.uwlax.cs.oayonlinestore.client.OnlinestoreRService;
import edu.uwlax.cs.oayonlinestore.client.utils.Currency;
import edu.uwlax.cs.oayonlinestore.client.utils.DateEdit;
import edu.uwlax.cs.oayonlinestore.client.utils.DefaultAsyncCallback;
import edu.uwlax.cs.oayonlinestore.client.utils.DekoTable;
import edu.uwlax.cs.oayonlinestore.client.utils.HSpacer;
import edu.uwlax.cs.oayonlinestore.client.utils.TitlePage;
import edu.uwlax.cs.oayonlinestore.client.utils.DateEdit.InvalidDateException;
import edu.uwlax.cs.oayonlinestore.vo.SalesReportDetailsVO;
import edu.uwlax.cs.oayonlinestore.vo.SalesReportProductVO;
import edu.uwlax.cs.oayonlinestore.vo.SalesReportVO;

public class PageSalSalesReport extends TitlePage {

	private class LoadDataHandler extends DefaultAsyncCallback implements
			ClickListener {

		public void onClick(Widget arg0) {
			try {
				OnlinestoreRService.Util.getInstance().getSalesReport(
						date.getDate(), this);
			} catch (InvalidDateException e) {
				Window.alert(e.getMessage());
			}
		}

		public void onSuccess(Object arg0) {
			table.setReport((SalesReportVO) arg0);
		}
	}

	private class SalesReportTable extends DekoTable {

		public SalesReportTable() {
			super();
			addTitle("Brand");
			addTitle("Product");
			addTitle("Quantity");
			addTitle("Price");
			addTitle("Discount");
			addTitle("Tax");
			addTitle("Total");
		}

		private void addItem(SalesReportProductVO p) {
			Iterator i = p.getDetails().iterator();
			AddRow r = addRow();
			r.add(p.getProduct().getBrand()).setRowspan(p.getDetails().size());
			r.add(p.getProduct().getName()).setRowspan(p.getDetails().size());
			addSellUnit(r, (SalesReportDetailsVO) i.next());
			r.add(new Currency(p.getTax())).setRowspan(p.getDetails().size());
			r.add(new Currency(p.getTotal())).setRowspan(p.getDetails().size());
			while (i.hasNext())
				addSellUnit(addRow(), (SalesReportDetailsVO) i.next());
		}

		private void addSellUnit(AddRow r, SalesReportDetailsVO u) {
			r.add(u.getQuantity());
			r.add(new Currency(u.getPrice()));
			r.add(u.getDiscount(), "%");
		}

		public void setReport(SalesReportVO report) {
			cleanExceptHeader();
			for (Iterator i = report.getProducts().iterator(); i.hasNext();)
				addItem((SalesReportProductVO) i.next());

			String s = "From: " + report.getFromDate() + "<br>To: "
					+ report.getToDate();
			AddRow r = addRow();
			r.addHtml(s).setColspan(5);
			r.add(new Currency(report.getTax())).bold();
			r.add(new Currency(report.getTotal())).bold();
		}

	}

	private DateEdit date;

	private SalesReportTable table;

	protected void create() {
		setTitle("Sales Report");
		add(createDateSel());
		add(new HSpacer());
		add(table = new SalesReportTable());
	}

	private Widget createDateSel() {
		HorizontalPanel panel = new HorizontalPanel();
		panel.setSpacing(10);
		panel.add(new HTML("Select Day:"));
		panel.add(date = new DateEdit(true, true));
		panel.add(new Button("Show", new LoadDataHandler()));
		return panel;
	}

	public String getID() {
		return "SalesReport";
	}

}
